<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./vue.js"></script>
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css">
    <style>
        img {
            width: 80px;
            height: 80px;
        }
    </style>
</head>

<body>
    <!-- 定义挂载点 -->
    <div id="app" class="container">
        <!-- 1.完成静态布局 -->
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th><input type="checkbox" v-model="allchecked" @change="changeAll"> 全选</th>
                    <th>商品信息</th>
                    <th>商品单价</th>
                    <th>商品数量</th>
                    <th>商品小计</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="(item,index) in arr" :key="item.id">
                    <td><input type="checkbox" v-model="item.ischecked" @change="changeOne"></td>
                    <td>
                        <strong>{{item.name}}</strong>
                        <img :src="item.img" alt="">
                    </td>
                    <td>{{item.price|filterFixed}}</td>
                    <td>
                        <button class="btn btn-default" @click="sub(index)">-</button>
                        {{item.num}}
                        <button class="btn btn-primary" @click="add(index)">+</button>
                    </td>
                    <td>{{item.price * item.num|filterFixed}}</td>
                    <td> <button class="btn btn-danger" @click="delItem(index)">删除</button></td>
                </tr>
                <tr>
                    <td>总价</td>
                    <td colspan="5">{{0|filterFixed}}</td>
                </tr>
            </tbody>
        </table>
    </div>

    <script>
        //5.定义一个全局的过滤器，小数保留两位
        Vue.filter("filterFixed", (p) => {
            return p.toFixed(2);
        })
        let mock = [{
            id: 1,
            img: "https://img10.360buyimg.com/n7/jfs/t1/116325/12/33822/74207/642ec4caF5b53c8a7/8c548ca792f592fc.jpg",
            name: "手机",
            price: 2999.99,
            num: 3
        },
        {
            id: 2,
            img: "https://img12.360buyimg.com/n7/jfs/t1/72688/18/26431/128682/6433e218F83aa27a3/6e79bf181755bf44.jpg.avif",
            name: "电脑",
            price: 13131.9,
            num: 1
        },
        {
            id: 3,
            img: "https://img14.360buyimg.com/n7/jfs/t1/105370/22/32732/131023/646effdaFdff68a3f/ffeecdc7067016bd.jpg.avif",
            name: "ipad pro",
            price: 5999,
            num: 1
        },
        ]
        new Vue({
            //挂载点
            el: "#app",
            //数据
            data: {
                //2.定义数据
                arr: [],
                allchecked:false
            },
            //方法
            methods: {
                //3.点击删除按钮，删除对应的商品
                delItem(index) {
                    this.arr.splice(index, 1)
                },
                //4.点击+按钮，数量+1
                add(index) {
                    
                    // this.arr[index].num < 5 ? ++this.arr[index].num : this.arr[index].num = 5
                    //边界值判断
                    this.arr[index].num = Math.min(5,++this.arr[index].num);
                },
                //6.点击-按钮 ，数量-1
                sub(index){
                    this.arr[index].num = Math.max(1,--this.arr[index].num);
                },
                //7.全选商品
                changeAll(){
                    //把全选按钮的状态，赋值下面的input
                    this.arr.forEach(value=>{
                        value.ischecked = this.allchecked
                    })
                },
                //9.点击input，判断是否全选
                changeOne(){
                    this.allchecked = this.arr.every(value=>{
                        return value.ischecked == true;
                    });
                    
                }
            },
            //挂载完成后开始请求数据
            mounted() {
               
                //先处理数据，然后再往data中放，这样就不会出现属性变化，页面不重新渲染的问题
                //8.给每一个商品都添加一个选中状态
                mock.forEach(value=>{
                    value.ischecked = false;
                })

                this.arr = mock;
            }
        })

        //那个生命周期中发ajax请求？
        //created,beforeMount,mounted都能发，我一般在mounted
    </script>
</body>

</html>